<?php

require_once( '../../conf/conf.inc' );
require_once( PATH_FRAME.'/etwork.php' );

$etw = new etwork;
$etw->parseURL('edit/member', 3);

$etw->limitLevel = access::MODERATOR;
$etw->isEdit = true;

if (! isset($etw->pathParameters[0]) ) {
    
    $etw->setTemplate('hito', TRUE, TRUE, 0);

    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
    ));
    $etw->setTitle(array('hito','list'));


    $loader = new modelLoader;
    $loader->model = 'hito';
    $loader->order = @$etw->EX_GET['r'] ? $etw->EX_GET['r'] : '_update_date';
    $loader->alike = @$etw->EX_GET['s'];
    $loader->offset = @$etw->EX_GET['p'];
    $loader->returning = 'list';
    $etw->addLoader($loader);

    $etw->loadData();

} else if (isNumber($etw->pathParameters[0])) {

    $etw->setTemplate( 'hito', true, false );
    $etw->addClearTemplate( null, 'hito|solo'.$etw->pathParameters[0]);

    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
        array('dir'=>'edit/hito', 'config'=>array('hito','list'))
    ));
    if ($etw->pathParameters[0] > 0)
        $etw->setTitle(array('edit'),array('solo'=>'name'));
    else
        $etw->setTitle(array('hito','newpost'));


    $etw->noCaching = true;

    $etw->jumpTo = URL_EDIT .'hito';

    $loader = new modelLoader;
    $loader->model = 'hito';
    $loader->solo = $etw->pathParameters[0];
    $loader->targetId = 'hito_id';
    $loader->returning = 'solo';
    $loader->isEdit = TRUE;
    $loader->mapperParameters = array(
        'editor_id' => $etw->getUserId()
    );
    $etw->addLoader($loader);

    //カテゴリ
    if ($etw->pathParameters[0] && !$etw->EX_POST) {
        
        $loader = new modelLoader;
        $loader->model = 'category';
        $loader->targetCustomFunction = 'updatePostCategory';
        $loader->targetChoiceFunction = 'findParameterByPost';
        $loader->targetId = 'hito_id';
        $loader->returning = 'list';
        $loader->isEdit = true;
        $loader->solo = $etw->pathParameters[0];
        $loader->formParameters = array('_category');
        $etw->addLoader($loader);
    }

    //カテゴリフォーム
    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetId = 'slug';
    $loader->solo = 'hito';
    $loader->targetChoiceFunction = 'findByPost';
    $loader->returning = 'listform';
    $etw->addLoader($loader);

    $etw->loadData();

} else if (isset($etw->pathParameters[1]) && isNumber($etw->pathParameters[1]) && $etw->pathParameters[0] == 'history'
           && isset($etw->pathParameters[2]) && isNumber($etw->pathParameters[2])) {

    $etw->setTemplate( 'hito', true, false );
    $etw->addClearTemplate( null, 'hito|solo'.$etw->pathParameters[0]);

    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
        array('dir'=>'edit/hito', 'config'=>array('hito','list')),
        array(
            'dir'=>'edit/hito/'.$etw->pathParameters[0].'/'.$etw->pathParameters[1],
            'config'=>array('edit','history'),
            'load'=>array('solo'=>'name')
        )
    ));
    $etw->setTitle(array('history','edit'),array('solo'=>'name'));

    $etw->jumpTo = URL_EDIT .'hito';

    $loader = new modelLoader;
    $loader->model = 'hito';
    $loader->solo = $etw->pathParameters[1];
    $loader->targetId = 'hito_id';
    $loader->mapperParameters = array(
        'history_id' => $etw->pathParameters[2]
    );
    $loader->isEdit = TRUE;
    $etw->addLoader($loader);

    //カテゴリ
    if (!$etw->EX_POST) {
        $loader = new modelLoader;
        $loader->model = 'category';
        $loader->targetCustomFunction = 'updatePostCategory';
        $loader->targetChoiceFunction = 'findParameterByPost';
        $loader->targetId = 'hito_id';
        $loader->returning = 'list';
        $loader->isEdit = true;
        $loader->solo = $etw->pathParameters[1];
        $loader->mapperParameters = array(
            'history_id' => $etw->pathParameters[2]
        );
        $loader->formParameters = array('_category');
        $etw->addLoader($loader);
    }

    //カテゴリフォーム
    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetId = 'slug';
    $loader->solo = 'hito';
    $loader->targetChoiceFunction = 'findByPost';
    $loader->returning = 'listform';
    $etw->addLoader($loader);
    
    $etw->loadData();

} else if (isset($etw->pathParameters[1]) && isNumber($etw->pathParameters[1]) && $etw->pathParameters[0] == 'history') {

    $etw->setTemplate('history', TRUE, TRUE, 0);
    $etw->setTemplateGroup('hito');
    
    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
        array('dir'=>'edit/hito', 'config'=>array('hito','list'))
    ));
    $etw->setTitle(array('edit','history'),array('list_count'=>'_name'));

    $loader = new modelLoader;
    $loader->model = 'hito';
    $loader->order = @$etw->EX_GET['r'] ? $etw->EX_GET['r'] : '_publish_date';
    $loader->alike = @$etw->EX_GET['s'];
    $loader->offset = @$etw->EX_GET['p'];
    $loader->targetSelectFunction = 'findHistoryById';
    $loader->returning = 'list';
    $loader->mapperParameters = array(
        'hito_id' => $etw->pathParameters[1]
    );
    $etw->addLoader($loader);

    $etw->loadData();

} else if (isset($etw->pathParameters[1]) && isNumber($etw->pathParameters[1]) 
           && in_array($etw->pathParameters[0], array('same','unit'))) {

    $etw->setTemplate('fook', TRUE, TRUE, 0);
    $etw->setTemplateGroup('link');

    $etw->jumpTo = URL_EDIT .'hito';
    
    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
        array('dir'=>'edit/hito', 'config'=>array('hito','list'))
    ));
    $etw->setTitle(array('same'),array('solo'=>'name'));

    $loader = new modelLoader;
    $loader->model = 'hito';
    $loader->solo = $etw->pathParameters[1];
    $loader->targetId = 'hito_id';
    $etw->addLoader($loader);

    $loader = new modelLoader;
    $loader->model = 'link';
    $loader->solo = $etw->pathParameters[0];
    $loader->targetId = 'slug';
    $loader->targetChoiceFunction = 'findBySlug';
    $loader->returning = 'select';
    $loader->mapperParameters = array(
        'slug'      => $etw->pathParameters[0],
    );
    $etw->addLoader($loader);

    $loader = new modelLoader;
    $loader->model = 'link';
    $loader->targetSelectFunction = 'findListBySlug';
    $loader->targetCustomFunction = 'updateEachLink';
    $loader->returning = 'link';
    $loader->isEdit = true;
    $loader->mapperParameters = array(
        'parent_id' => $etw->pathParameters[1],
        'editor_id' => $etw->getUserId(),
        'slug'      => $etw->pathParameters[0],
        'target'    => 'hito'
    );
    if ($etw->pathParameters[0] == 'same') {
        $loader->addMapperParameter('each', true);
    }
    $etw->addLoader($loader);

    $etw->viewAssign('target', 'hito');
    
    $etw->loadData();
}

$etw->showFinal();


